package level.editor.control.main;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JPanel;

import level.editor.control.collision.CollisionHandler;
import level.editor.control.select.SelectHandler;
import level.editor.control.tile.TileHandler;

public class CurrentTool extends JPanel
{
	public CurrentTool(Main main)
	{
		super();
		setOpaque(false);
		this.main = main;
	}

	@Override
	public void paintComponent(Graphics gOld)
	{
		setSize(main.getSize());
		Graphics2D g = (Graphics2D) gOld;
		g.setStroke(new BasicStroke(2f));
		Rectangle vis = getVisibleRect();
		Rectangle rect = new Rectangle((int) (vis.getX() + vis.getWidth()
				- WIDTH - X_OFFSET), (int) (vis.getY() + vis.getHeight()
				- HEIGHT - Y_OFFSET), WIDTH, HEIGHT);

		paintTool(g, rect);
	}

	private void paintTool(Graphics2D g, Rectangle rect)
	{
		Font textFont = new Font("Arial", Font.BOLD, 13);
		g.setFont(textFont);
		paintLayerName(g, rect);

		if (main.getHandler() != null)
		{
			paintHandler(g, rect);
		}
	}

	private void paintLayerName(Graphics2D g, Rectangle rect)
	{
		String layerName = "Layer:  ";
		g.setColor(Color.GRAY);

		if (main.getHandler() == null
				|| !(main.getHandler() instanceof CollisionHandler))
		{
			if (main.getHandler() instanceof TileHandler)
			{
				if (main.getSelectedLayer() == null)
					layerName += "No layer selected";
				else if (main.getSelectedLayer().isLocked())
					layerName += "Layer is LOCKED";
				else
				{
					g.setColor(Color.WHITE);
					layerName += main.getSelectedLayer().getName();
				}
			}
			else if (main.getHandler() instanceof SelectHandler)
			{
				g.setColor(Color.WHITE);
				layerName = "Layers: All unlocked layers";
//				for (Layer layer : main.getUnlockedLayers())
//				{
//					layerName += layer.getName() + "\n";
//				}
//				layerName = layerName.substring(0, layerName.length() - 2);
			}
		}
		else if (main.getHandler() != null
				&& main.getHandler() instanceof CollisionHandler)
		{
			g.setColor(Color.WHITE);
			layerName += "Collision Layer";
		}

		g.fill(rect);

		g.setColor(Color.DARK_GRAY);
		g.draw(rect);

		g.setColor(Color.BLACK);
		g.setStroke(new BasicStroke(4f));
		g.drawString(layerName, (int) (rect.getX() + LAYERNAME_X_OFFSET),
				(int) (rect.getY() + LAYERNAME_Y_OFFSET));

	}

	private void paintHandler(Graphics2D g, Rectangle rect)
	{
		main.getHandler().paint(
				g,
				new Point((int) (rect.getX() + TOOL_X_OFFSET), (int) (rect
						.getY() + TOOL_Y_OFFSET)));
	}

	private Main main;
	private static final int WIDTH = 200;
	private static final int HEIGHT = 200;
	private static final int X_OFFSET = 20;
	private static final int Y_OFFSET = 20;
	private static final int LAYERNAME_X_OFFSET = 10;
	private static final int LAYERNAME_Y_OFFSET = 20;
	private static final int TOOL_Y_OFFSET = 60;
	private static final int TOOL_X_OFFSET = 10;
}
